<template>
  <div class="add-wrap">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="70px"
      class="demo-ruleForm">
      <el-form-item label="用户名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm()">立即创建</el-button>
        <el-button @click="$emit('close')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data () {
    return {
      ruleForm: {
        name: '',
        password: ''
      },
      rules: {
        name: [{required: true}],
        password: [{required: true}],
      }
    }
  },
  methods: {
    async submitForm () {
      try {
        if (!await this.$refs.ruleForm.validate()) {
          return
        }
        let res = await this.axios.post("/user/create", this.ruleForm)
        if (!res.data) {
          return
        }
        this.$message({
          message: '创建成功',
          type: 'success'
        });
        this.$emit('close')
        
      } catch (error) {
        console.log(error)
      }
    },
  },
}
</script>

<style scoped>
.add-wrap {
  padding: 10px;
}
</style>